//
//  JSCityListViewController.m
//  NiubilityProject
//
//  Created by Apple on 2017/11/20.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "JSCityListViewController.h"
#import "JSSeetingUserInfoVC.h"
#import "JSCityListCell.h"
#import "JSCountryInfoModel.h"
@interface JSCityListViewController ()
@property(nonatomic, strong) NSMutableArray<JSCountryInfoModel *> *searchResultsKeywords;

@end

@implementation JSCityListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.extendedLayoutIncludesOpaqueBars = YES;

}
- (UIImage *)navigationBarShadowImage{
    return [UIImage qmui_imageWithColor:JS_colorWithHex(0xe7e7e7) size:CGSizeMake(1, flat(0.5)) cornerRadius:0];
}
- (void)didInitialized{
    [super didInitialized];
    self.view.backgroundColor =JS_colorWithHex(kBodyBGColor);
    self.mouth  = @"getCity";
    JS_WeakSelf(self)
    [self setResponse:^(id response, BOOL isNewData) {
        if ([response[@"status"] integerValue] == 200 && ValidArray(response[@"data"][@"list"])) {
            for (NSDictionary *dic in response[@"data"][@"list"]) {
                JSCountryInfoModel *m = [JSCountryInfoModel mj_objectWithKeyValues:dic];
                [weakself.dataSource addObject:m];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakself.tableView reloadData];
            });
        }else{
            [JSAlertHelper showFailHideAfterDelay:1.5 text:JSLocalizedString(kLocalizableServerError) inView:weakself.view animation:YES];
        }
    }];
    [self.tableView.mj_header beginRefreshing];
}
- (void)initTableView{
    [super initTableView];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5)];
    headerView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = headerView;
    self.tableView.mj_footer = nil;
//    self.tableView.mj_header = nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.tableView) {
        return self.dataSource.count;
    }
    return self.searchResultsKeywords.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JSCityListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[JSCityListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.textLabel.font = UIFontMake(14);
        cell.textLabel.textColor = [UIColor blackColor];
        
    }
    NSString *Key = nil;
    if (indexPath.row < self.dataSource.count) {
        JSCountryInfoModel *m = self.dataSource[indexPath.row];
        if (JSLocalizeIsChina()) {
            Key = m.state_zh;
        }else{
            Key = m.state;
        }
    }
    if (tableView == self.tableView) {
        cell.textLabel.text = Key;
    }else{
        JSCountryInfoModel *m = self.searchResultsKeywords[indexPath.row];
        if (JSLocalizeIsChina()) {
            Key = m.state_zh;
        }else{
            Key = m.state;
        }
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:Key attributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];
        NSRange range = [Key rangeOfString:self.searchController.searchBar.text];
        if (range.location != NSNotFound) {
            [attributedString addAttributes:@{NSForegroundColorAttributeName: [QDThemeManager sharedInstance].currentTheme.themeTintColor} range:range];
        }
        cell.textLabel.attributedText = attributedString;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableView) {
        if (self.didSelectCity) {
            self.didSelectCity(self.dataSource[indexPath.row]);
        }
    }else{
        if (self.didSelectCity) {
            self.didSelectCity(self.searchResultsKeywords[indexPath.row]);
        }
    }
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)searchController:(QMUISearchController *)searchController updateResultsForSearchString:(NSString *)searchString{
    [self.searchResultsKeywords removeAllObjects];
    NSString *Key = nil;
    for (JSCountryInfoModel *model in self.dataSource) {
        if (JSLocalizeIsChina()) {
            Key = model.state_zh;
        }else{
            Key = model.state;
        }
        if ([Key qmui_includesString:searchString]) {
            [self.searchResultsKeywords addObject:model];
        }
    }
    [searchController.tableView reloadData];
    if (self.searchResultsKeywords.count == 0) {
        [searchController showEmptyViewWithText:JSLocalizedString(kLocalizableSearchResult) detailText:nil buttonTitle:nil buttonAction:NULL];
    } else {
        [searchController hideEmptyView];
    }
}

- (void)willPresentSearchController:(QMUISearchController *)searchController{
    self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    self.tableView.contentOffset= CGPointMake(0, -64);
}
- (void)didDismissSearchController:(QMUISearchController *)searchController{
    JSLog(@"kTopSpace %zd", kTopSpace);
    self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    self.tableView.contentOffset= CGPointMake(0, -64);
}
- (NSMutableArray *)searchResultsKeywords{
    if (!_searchResultsKeywords) {
        _searchResultsKeywords = @[].mutableCopy;
    }
    return _searchResultsKeywords;
}
@end
